package com.eportal.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.eportal.action.background.TOpenLogAction;
import com.eportal.domain.Sysopt;
import com.eportal.params.background.TOpenlog.TOpenLogParamList;
import com.eportal.service.TOpenLogService;
import com.eportal.vo.controller.background.topenlog.TOpenLogListRow;
import com.eportal.vo.controller.background.topenlog.TOpenLogListVO;
import com.framework.jqgrid.Page;
import com.framework.jqgrid.PageEntity;
import com.framework.utils.excel.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Saber on 2017/8/7.
 */
@Controller
@RequestMapping(value = "/admin/openlog")
public class TOpenLogController {

    private static Logger logger = LoggerFactory.getLogger(TOpenLogController.class.getName());

    @Resource
    private TOpenLogService tOpenLogService;


    /**
     * 开门日志列表
     * @author Saber
     * @Date 2017/8/8 上午9:06
     * @param request
     * @param pageEntity    分页对象
     * @param param         参数
     * @see TOpenLogAction#page()
     * @return
     */
    @RequestMapping(value = "page.json")
    @ResponseBody
    public JSONObject page(HttpServletRequest request,PageEntity pageEntity,TOpenLogParamList param){
        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        JSONObject json = new JSONObject();

        try {
            PageInfo<TOpenLogListVO> pageInfo=tOpenLogService.queryForPage(pageEntity,param,user,userRole);

            json.put("total",pageInfo.getTotal());

            if(pageInfo.getList().size()==0){
                json.put("msg","暂无数据");
            }else{
                json.put("msg","ok");
            }
            json.put("data",pageInfo.getList());
            Page page = new Page(pageInfo,pageEntity.getPageNo());
            json.put("page", page);
            json.put("code",1);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("门禁管理列表异常:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }

    }

    /**
     * 导出excel
     * @author Saber
     * @Date 2017/8/9 下午12:01
     * @param request
     * @param param     参数
     * @see TOpenLogAction#exportExcel()
     * @return
     */
    @RequestMapping(value = "exportExcel.json")
    @ResponseBody
    public JSONObject exportExcel(HttpServletRequest request,HttpServletResponse response,TOpenLogParamList param){
        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        JSONObject json = new JSONObject();

        try {
            List<TOpenLogListRow> list = tOpenLogService.exportExcel(param, user, userRole);
            if(list.size()>3000){
                json.put("code", 0);
                json.put("msg", "导出的数据不能超过3千条");
                json.put("data",list.size());
                return json;
            }
            ExcelUtil<TOpenLogListRow> excelUtil = new ExcelUtil<>(TOpenLogListRow.class);
            //设置默认文件名为当前时间：年月日时分秒
            String fileName=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()).toString();

            //设置response头信息
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");        //改成输出excel文件
            response.setHeader("Content-disposition","attachment; filename="+fileName+".xls" );
            boolean flag = excelUtil.exportExcel(list,"开门日志",65536,response.getOutputStream());
            if(flag){
                json.put("code",1);
                json.put("msg", "导出成功");
            }else {
                json.put("code",0);
                json.put("msg", "导出失败");
            }
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("门禁管理列表异常:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }

    }


    /**
     * 导出excel检查
     *
     * @param request
     * @param param   参数
     * @return
     * Created by hongyy on 2017/8/17.
     * @see TOpenLogAction#exportExcelCheck()
     */
    @RequestMapping(value = "exportExcelCheck.json")
    @ResponseBody
    public JSONObject exportExcelCheck(HttpServletRequest request, HttpServletResponse response, TOpenLogParamList param) {

        JSONObject json = new JSONObject();
        try{
        Sysopt user = (Sysopt) request.getSession().getAttribute("user");
        int userRole = (Integer) request.getSession().getAttribute("userRole");
        List<TOpenLogListRow> list = tOpenLogService.exportExcel(param, user, userRole);

        if(list.size()>3000){
            json.put("code", 0);
            json.put("msg", "导出的数据不能超过3千条");
            json.put("data",list.size());
            return json;
        }else {
            json.put("code", 1);
            json.put("msg", "正在导出，请稍候，日志数量："+list.size()+" 条");
            json.put("data",list.size());
        }
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("门禁管理列表异常:", e);
            json.put("code", 0);
            json.put("msg", "服务器发生异常:" + e.getMessage());
            json.put("data", null);
            return json;
        }
        }
    }

